﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Xml;

namespace CoreShop.App
{
    public class MTscale
    {
        [DllImport("dll\\MTScaleAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        private extern static string ExecuteTaskInString(string szTaskID, string szInput, bool bSynch);
        [DllImport("dll\\MTScaleAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern bool ExecuteTaskInFile(string szTaskID, string szInputFile, string szOutputFile, bool bSynch);
        [DllImport("dll\\MTScaleAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        private extern static IntPtr QueryTask(string szInput);
        [DllImport("dll\\MTScaleAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        private extern static string CancelTask(string szInput);
        [DllImport("dll\\MTScaleAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        private extern static void Free(object p);
        [DllImport("dll\\MTScaleAPI.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        private extern static void Release();


        /*
            <?xml version="1.0" encoding="utf-8" ?>
            <root>
	            <MTTask>
		            <TaskID>123456789</TaskID>
		            <TaskType>95</TaskType>
	            </MTTask>
            </root>
         */
        public static string GetScaleList(string szInput)
        {
            string szTaskID = Common.GetTaskID();
            string str = "";    // ExecuteTaskInString(szTaskID, szInput, true);
            bool bResult = ExecuteTaskInFile(szTaskID, @"E:\newproj\TestTask\TestTask\bin\Debug\xml\getScaleList.xml", @"E:\newproj\TestTask\TestTask\bin\Debug\xml\TaskResult.xml", false);
            IntPtr p = QueryTask(szTaskID);
            //Free(p);
            return str;
        }

        public static string SendScale()
        {
            string CurrentPath = Environment.CurrentDirectory;
            string resultFile = CurrentPath + "\\xml\\TaskResult.xml";
            if (System.IO.File.Exists(resultFile))
                System.IO.File.Delete(resultFile);

            bool bResult = ExecuteTaskInFile(BillXml.TaskID, CurrentPath + "\\xml\\Task.xml", resultFile, false);
            if (!bResult)
            {
                return "Call ExecuteTaskInFile Error";
            }

            //查询任务状态来获取任务结果
            string strQueryTaskInput = "<MTTask><TaskID>" + BillXml.TaskID + "</TaskID><TaskType>98</TaskType></MTTask>";
            while (true)
            {
                //调用查询任务状态方法。
                IntPtr p = QueryTask(strQueryTaskInput);
                string strTmp = Marshal.PtrToStringAnsi(p);
                //释放动态库返回的字符串资源
                Free(p);

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(strTmp);

                if (xmlDoc.SelectSingleNode("MTTaskResult") != null)
                {
                    if (xmlDoc.SelectSingleNode("MTTaskResult").SelectSingleNode("TaskStatus") != null)
                    {

                        if (xmlDoc.SelectSingleNode("MTTaskResult").SelectSingleNode("TaskStatus").InnerText.Equals("Complete", StringComparison.OrdinalIgnoreCase) ||
                            xmlDoc.SelectSingleNode("MTTaskResult").SelectSingleNode("TaskStatus").InnerText.Equals("Error", StringComparison.OrdinalIgnoreCase))
                        {                            
                            //任务执行结束
                            break;
                        }
                    }
                }
            }

            //读取文件来获取任务执行的结果。
            string strResult = "";
            using (System.IO.StreamReader sr = new System.IO.StreamReader(resultFile))
            {
                strResult = sr.ReadToEnd();
                sr.Close();
            }

            return strResult;
        }
    }
}
